package com.tmeone.erpone.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tmeone.erpone.entity.JsonResult;
import com.tmeone.erpone.entity.TWare;
import com.tmeone.erpone.entity.WareNoAndTime;
import com.tmeone.erpone.service.ITWareService;
import org.beetl.ext.fn.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  仓库控制器
 * </p>
 *
 * @author wzy
 * @since 2021-04-19
 */
@RestController
public class TWareController {

    @Autowired
    private ITWareService itWareService;

    /**
     * 查询全部
     * @return JsonResult
     */
    @GetMapping("/warehouses")
    public JsonResult selectListAll(){
        return new JsonResult(1, itWareService.list());
    }


    @RequestMapping(value = "/wares/{current}/{pageSize}",method = RequestMethod.POST,consumes = "application/json;charset=UTF-8")
    public JsonResult findWareMassage(@PathVariable(value = "current",required = false)Integer current, @PathVariable(value = "pageSize",required = false)Integer pageSize,@RequestBody WareNoAndTime wareNoAndTime){

        Page<TWare> page = new Page<>(current,pageSize);
       if(wareNoAndTime!=null){
           IPage<TWare> tWareIPage = itWareService.warePage(page,wareNoAndTime);
           return new JsonResult(1,tWareIPage);
       }else {
           IPage<TWare> tWareIPage = itWareService.warePage(page, null);
           return new JsonResult(1,tWareIPage);
       }

    }

    /**
     * 根据wareNo查询仓库
     * @return
     */
    @GetMapping("/warehouses/{wareName}")
    public JsonResult selectWareById(@PathVariable String wareName) {
        return new JsonResult(1, itWareService.getOne(new QueryWrapper<TWare>().lambda()
        .eq(TWare::getWareName,wareName)));
    }

    @PostMapping("/ware")
    public JsonResult addWare(@RequestBody TWare ware){
        LocalDate now =  LocalDate.now();
        ware.setDeteTime(now);
        ware.setStatus("0");
        itWareService.save(ware);
        return new JsonResult(1,null);
    }

    //修改状态
    @PutMapping("/ware/{id}/{status}")
    public void updateWare(@PathVariable("id")String id,@PathVariable("status")String status){
        itWareService.updateWareStatusById(id,status);
    }

    //修改仓库功能
    @PutMapping("/ware/{id}")
    public void updateWareById(@PathVariable("id")String id,@RequestBody TWare tWare){
        itWareService.update(tWare,new QueryWrapper<TWare>().lambda().eq(TWare::getId,id));
    }

    @RequestMapping("/selectWareCargo")
    public JsonResult selectWareCargo(){
        List<TWare> tWares = itWareService.selectWareCargo();
        return new JsonResult(1,tWares);
    }
    @RequestMapping("/selectTwareOne")
    public JsonResult selectTwareOne(@RequestParam(value = "current",required = false,defaultValue = "1")Integer current,
                                     @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize,
                                     @RequestParam(value = "id",required = false)String id){
        Page<TWare> page = new Page<>(current, pageSize);
        IPage<TWare> tWareIPage = itWareService.selectWareCargoPageById(page, id);
        return new JsonResult(1,tWareIPage);
    }
    @RequestMapping("/selectTwareShelvesOne")
    public JsonResult selectTwareShelvesOne(String sId,String goodsName){
        TWare tWare = itWareService.selectTwareShelvesOne(sId, goodsName);
        ArrayList<TWare> list = new ArrayList<>();
        list.add(tWare);
        return new JsonResult(1,list);
    }
    @RequestMapping("/selectTwareAllwareName")
    public JsonResult selectTwareAllwareName(){
        List<TWare> list1 = itWareService.list();
        return new JsonResult(1,list1);
    }
    @RequestMapping("/t-ware/shelves")
    public JsonResult selectShelvesByAll1(String shelvesNo){
        List<TWare> tWares = itWareService.selectShelvesByAll1(shelvesNo);
        return new JsonResult(1,tWares);
    }

}
